<template>
  <div
    class="
      alarm-record
      flex flex-direction
      strut-absolute
      margin-top-xl
      padding-lr-xl padding-bottom-xs
    "
  >
    <el-form
      class="form_password"
      :model="parameters"
      label-width="100px"
      :rules="rules"
      ref="parameters"
    >
      <el-form-item label="账户名:" prop="Name">
        <el-input
          type="text"
          placeholder="请输入账户名"
          v-model="parameters.Name"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="旧密码：" prop="OldPassword">
        <el-input
          type="password"
          placeholder="请输入旧密码"
          v-model="parameters.OldPassword"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="新密码：" autocomplete="off" prop="NewPassword">
        <el-input
          type="password"
          placeholder="请输入新密码"
          v-model="parameters.NewPassword"
          clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item label="确认密码：" autocomplete="off" prop="checkPassword">
        <el-input
          type="password"
          placeholder="请确认密码"
          v-model="parameters.checkPassword"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-button
        type="primary"
        style="width: 30%; margin-left: 15%"
        @click="updatePassword(parameters)"
        >更改</el-button
      >
      <el-button @click="resetForm('parameters')">重置</el-button>
    </el-form>
  </div>
</template>

<script>
import { modifyPassword } from '@/api/venue'

export default {
  data() {
    return {
      parameters: {
        Name: '',
        OldPassword: '',
        NewPassword: '',
        checkPassword: ''
      },

      rules: {
        Name: [{ required: true, message: '请输入账户名', trigger: 'blur' }],
        OldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        NewPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '请确认新密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async updatePassword(form) {
      if (this.parameters.NewPassword !== this.parameters.checkPassword) {
        alert('新密码两次输入不一样！请检查密码！')
        return
      }
      this.parameters.checkPassword = null
      await modifyPassword(form)
    },

    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style>
.form_password {
  text-align: center;
  width: 30%;
  margin-left: 25%;
  top: 10%;
}
</style>
